/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

// Start the main app logic.
requirejs([
    '../API/API',
    'models/contact',
    'views/Title'
],function(){
    
    var contact  = new API.model.BasicModel({    
        defaults : {
            name : '',
            direccion : '',
            celular : '',
            telefono : '',
            email : ''
        }
    });
    
    //console.log(contact.getAttributesNames())
    
    var store = new API.store.BasicStore({
        
    });   
    
    var onEventChange = function(){        
        var s = selector;
        var st = store;
        var f = form;
        var g = grid;
        var b = formButton;
        
        if(API.isZero(s.getValue())){
            f.disableEdit();
            f.clear();
            b.disable();
        }
        else{
            b.enable();
            f.setValues( st.getValueById( s.getValue() ) );
            if(f.getAction() === 'update')
                f.enableEdit();
        }

        g.refresh();
    };
    
    var selector = new API.form.Selector({
        store: store,
        name: 'id',
        value: 'id',
        description: 'name',
        text: 'asd',
        defaultValue: 0,
        defaultDescription: 'Seleccione un Nombre...',
        ignoreSet: true,
        listeners: {
            change: onEventChange
        }          
    });
    
    var form = new API.form.BasicForm({
                
        formAction: 'create',

        setAction : function(action){
            this.clear();

            if(action === 'create'){
                selector.hide();
                this.enableEdit();
            }else if(action === 'update' || action === 'delete'){
                selector.load();
                selector.show();
                this.disableEdit(); 
            };

            return this.formAction = action;
        },


        getAction : function(){
            return this.formAction;
        },         

        disableEdit : function(){
            var items = this.getItems();
            items['name'].disable();
            items['direccion'].disable();
            items['celular'].disable();
            items['telefono'].disable();
            items['email'].disable();
        },

        enableEdit : function(){
            var items = this.getItems();
            items['name'].enable();
            items['direccion'].enable();
            items['celular'].enable();
            items['telefono'].enable();
            items['email'].enable();
        },

        items: [
            selector,
            new API.form.Input({
                name: 'name',
                text: 'Nombre: ',
                id  : 'form-name'
            }),
            new API.form.Input({
                name: 'direccion',
                text: 'Dirección: ',
                id  : 'form-direccion'
            }),
            new API.form.Input({
                name: 'celular',
                text: 'Nro de Celular: ',
                id  : 'form-celular'
            }),
            new API.form.Input({
                name: 'telefono',
                text: 'Nro Fijo: ',
                id  : 'form-telefono'
            }),
            new API.form.Input({
                name: 'email',
                text: 'Correo Electrónico: ',
                id  : 'form-email'
            })
        ]
    });
    
    var formButton = new API.button.BasicButton({
        text: 'Guardar',
        class: 'basic-form-submit-button',
        listeners:{
            click: function(){
                doAction();
            }
        }
    });
    
    var window = new API.window.BasicWindow({
        title: 'A title',
        items: [
            form,
            formButton,
            new API.button.BasicButton({
                text: 'Cerrar',
                class: 'basic-form-close-button',
                listeners:{
                    click: function(){
                        window.hide();
                    }
                }
            })
        ]
    });
    
    var grid = new API.grid.BasicGrid({
        store: store
    });
    
    var showWindow = function(formAction){
        
        switch(formAction){
            case 'create':
                window.setTitle('Nuevo registro');
                formButton.setText('Guardar');
                formButton.enable();
                form.enableEdit();
                break;
            case 'delete':
                window.setTitle('Eliminar registro');
                formButton.setText('Eliminar');
                formButton.disable();
                form.disableEdit();
                break;
            case 'update':
                window.setTitle('Modificar registro');
                formButton.setText('Guardar');
                formButton.disable();
                form.disableEdit();
                break;
        }
        
        form.setAction(formAction);  
        window.show();
        
    };
    
     var createRow = function(){        
        store.addValue(form.getValues());
    };
    
    var deleteRow = function(){          
       var values = form.getValues();
       store.remove(values.id);
    };
    
    var updateRow = function(){          
        deleteRow();
        createRow();        
    };
    
    var doAction = function(){
        
        switch(form.getAction()){
            case 'create':
                createRow();
                break;
            case 'delete':
                deleteRow();
                break;
            case 'update':
                updateRow();
                break;
        }
        
        grid.refresh();
        window.hide();
    };
    
    new API.viewport.BorderViewport({
        north: [
            new Title({
                text: 'Ejercicio Nivel 2'
            })
        ],
        center: [
            new API.toolbar.BasicToolbar({
                items: [
                    new API.button.BasicButton({
                        text: 'Alta',
                        class: 'toolbar-button',
                        listeners:{
                            click: function(){
                                showWindow('create');
                            }
                        }
                    }),
                    new API.button.BasicButton({
                        text: 'Modificación',
                        class: 'toolbar-button',
                        listeners:{
                            click: function(){
                                showWindow('update');
                            }
                        }
                    }),
                    new API.button.BasicButton({
                        text: 'Baja',
                        class: 'toolbar-button',
                        listeners:{
                            click: function(){
                                showWindow('delete');
                            }
                        }
                    })
                ]
            }),
            grid
        ]
    });
});
